#ifndef __Led_H
#define __Led_H

#ifdef APM32E10X_HD
	#include "apm32e10x_gpio.h"
	#include "apm32e10x_rcm.h"
#elif defined AT32F403AVGT7
	#include "at32f403a_407_gpio.h"
	#include "at32f403a_407_crm.h"
#else
	#include "apm32f10x_gpio.h"
	#include "apm32f10x_rcm.h"
#endif
#include "BoardData.h"
#if defined SQDri_EVSEB_7KW || defined SQDri_EVSEB_11KW
	#include "tools.h"
#endif
#include "LedPanel.h"

//////////////////////// led引脚MCU上拉控制 /////////////////////////
#define  Led_pullup       1       // >0 使能上拉


//引脚块1 = 接引脚块B = PB7 PB8 PB9
#if CHGR_panel_Sel == CHGR_panel_B26  
#define Led_RTC             Led1_RTC  
#define Led_PinPort         Led1_PinPort   
#define Led1_PinId          Led1_2PinId    
#define Led2_PinId          Led1_3PinId    
#define Led3_PinId          Led1_4PinId   
#define Led_All             Led1_PinId | Led2_PinId | Led3_PinId 
#elif CHGR_panel_Sel == CHGR_panel_B20 || CHGR_panel_Sel == CHGR_panel_B14  
#define Led_RTC             Led1_RTC  
#define Led_PinPort         Led1_PinPort
#define BL_PinId            Led1_1PinId       //7个红灯  
#define SDA_PinId           Led1_2PinId       //电源灯   ：SDA  
#define SCLK_PinId          Led1_3PinId       //充电灯绿 ：SCLK 
#define RCLK_PinId          Led1_4PinId       //充电灯蓝 ：RCLK 
#elif CHGR_panel_Sel == CHGR_panel_A12 
#define Led_RTC             Led2_RTC 
#define Led_PinPort         Led2_PinPort 
#define BL_PinId            Led2_4PinId        //7个红灯 
#define SDA_PinId           Led2_3PinId        //SDA   
#define SCLK_PinId          Led2_2PinId        //SCLK   
#define RCLK_PinId          Led2_1PinId        //RCLK  

#endif

#if Led_Key_Ctrl == 1
// 按键灯 = PB9
#define Led_Key_RTC         RCM_APB2_PERIPH_GPIOB 
#define Led_Key_PinPort     GPIOB
#define Led_Key_PinId       GPIO_PIN_9
#endif


// 灯控制函数
#if (CHGR_panel_Sel == CHGR_panel_B26)
// B26板子和其他板子相反。
#define led_off(x)            Led_PinPort -> BSC = x 
#define led_on(x)             Led_PinPort -> BC  = x 
#else
#define led_on(x)             Led_PinPort -> BSC = x 
#define led_off(x)            Led_PinPort -> BC  = x 
#endif

// 置位函数
#ifdef Using_LedCtrl_74HC595
#define led_bit_set(x)          Led_PinPort -> BSC = x 
#define led_bit_reset(x)        Led_PinPort -> BC  = x 
#endif

// 按键灯控制函数
#if Led_Key_Ctrl == 1
#define led_key_on()            Led_Key_PinPort -> BSC = Led_Key_PinId 
#define led_key_off()           Led_Key_PinPort -> BC  = Led_Key_PinId 
#endif

////////   常量   //////////
#define   led595AllOn         0x00
#define   led595AllOff        0xff

//单个灯亮：	
//A最靠近板子边缘
#ifdef  Led3Pin_MCUPin
	//对应Led灯列表;
	#define   led595On_A         Led1_PinId
	#define   led595On_B         Led2_PinId
	#define   led595On_C         Led3_PinId  
#else
  #define   led595On_A          0x7F
	#define   led595On_B          0xBF
	#define   led595On_C          0xDF
	#define   led595On_D          0xEF
	#define   led595On_E          0xF7
	#define   led595On_F          0xFB
	#define   led595On_G          0xFD
	#define   led595On_H          0xFE

#endif


#if I_rated_count ==  1
	#define   led_default        led595On_A  	
#endif


//位值 = 128,64,32,16,8,4,2,1
//滚动Led码:滚动总led=7个;暗4个亮3个;1=第一个。4=4个1
#define   led595_7roll4_1          0x78
#define   led595_7roll4_2          0xF0
#define   led595_7roll4_3          0xE2
#define   led595_7roll4_4          0xC6
#define   led595_7roll4_5          0x8E
#define   led595_7roll4_6          0x1E
#define   led595_7roll4_7          0x3C
//滚动Led码:滚动总led=5个;暗3个亮2个;1=第一个。3=3个1
#define   led595_5roll3_1          0x70
#define   led595_5roll3_2          0xE0
#define   led595_5roll3_3          0xC8
#define   led595_5roll3_4          0x98
#define   led595_5roll3_5          0x38
//--------------------------------------------------//
#define   led595_5roll4_1          0xF0
#define   led595_5roll4_2          0x78
#define   led595_5roll4_3          0xB8
#define   led595_5roll4_4          0xD8
#define   led595_5roll4_5          0xE8


////////   全局变量   //////////
#if I_rated_count >  1
	#ifdef Using_LedCtrl_MCUPin
	extern uint16_t Led_idx_List[I_rated_count]; 
	#elif (defined Using_LedCtrl_74HC595)
	extern uint8_t Led595_List[I_rated_count]; 
	#endif
#else  
  #ifdef Using_LedCtrl_MCUPin
  extern uint16_t  Led_idx_scroll;
	#endif
#endif


///// 函数声明 /////
void led_init(void);

#ifdef Using_LedCtrl_74HC595
// 发送字节;通过595控制亮灯;
void Led595_start(uint8_t bitData);
#endif

#endif


